package com.codelazy.desetalk;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.desetalk.service.NettyService;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.liyh.widget.toRoundBitmap;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.bean.SocializeUser;
import com.umeng.socialize.controller.RequestType;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.FetchUserListener;
import com.umeng.socialize.controller.listener.SocializeListeners.SocializeClientListener;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends FragmentActivity {
	private SlidingMenu menu;
	private boolean loginflag = false;
	private ImageView s_menu;
	private ImageView rimg;
	private Fragment[] mFragments;
	private RadioGroup menubtns;
	private FragmentManager fragmentManager;
	private TextView tv_nick;
	private FragmentTransaction fragmentTransaction;
	private RadioButton rbOne, rbTwo, rbThree, rbFour, rbFive;
	final UMSocialService mController = UMServiceFactory.getUMSocialService(
			"com.umeng.share", RequestType.SOCIAL);

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		initSlidingMenu();
		initFragment();
		setFragmentIndicator();
		setFragmentFun();

		if (umlogin()) {
			umgetInfo();
		} else {

		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private void initSlidingMenu() {
		setContentView(R.layout.activity_main);
		menu = new SlidingMenu(this);
		menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		menu.setShadowWidthRes(R.dimen.shadow_width);
		menu.setShadowDrawable(R.drawable.shadow);
		menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		menu.setFadeDegree(0.35f);
		menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		menu.setFadeEnabled(true);
		menu.setMenu(R.layout.menu_frame);
		Bitmap bmBitmap = toRoundBitmap.getbit(BitmapFactory.decodeResource(
				getResources(), R.drawable.tou));
		ImageView rimg = (ImageView) findViewById(R.id.rimg);
		rimg.setImageBitmap(bmBitmap);
	}

	private void initFragment() {
		mFragments = new Fragment[5];
		fragmentManager = getSupportFragmentManager();
		mFragments[0] = fragmentManager.findFragmentById(R.id.fragement_lobby);
		mFragments[1] = fragmentManager.findFragmentById(R.id.fragement_rank);
		mFragments[2] = fragmentManager.findFragmentById(R.id.fragement_bbs);
		mFragments[3] = fragmentManager.findFragmentById(R.id.fragement_shop);
		mFragments[4] = fragmentManager.findFragmentById(R.id.fragement_set);

		fragmentTransaction = fragmentManager.beginTransaction()
				.hide(mFragments[0]).hide(mFragments[1]).hide(mFragments[2])
				.hide(mFragments[3]).hide(mFragments[4]);
		fragmentTransaction.show(mFragments[0]).commit();
	}

	private void setFragmentFun() {
		for (int i = 0; i < mFragments.length; i++) {
			s_menu = (ImageView) mFragments[i].getView().findViewById(
					R.id.s_menu);
			s_menu.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					menu.showMenu();
				}
			});
		}
	}

	private void setFragmentIndicator() {

		menubtns = (RadioGroup) findViewById(R.id.menubtns);
		rbOne = (RadioButton) findViewById(R.id.rbOne);
		rbTwo = (RadioButton) findViewById(R.id.rbTwo);
		rbThree = (RadioButton) findViewById(R.id.rbThree);
		rbFour = (RadioButton) findViewById(R.id.rbFour);
		rbFive = (RadioButton) findViewById(R.id.rbFive);

		menubtns.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				fragmentTransaction = fragmentManager.beginTransaction()
						.hide(mFragments[0]).hide(mFragments[1])
						.hide(mFragments[2]).hide(mFragments[3])
						.hide(mFragments[4]);
				switch (checkedId) {
				case R.id.rbOne:
					fragmentTransaction.show(mFragments[0]).commit();
					menu.showContent();
					break;

				case R.id.rbTwo:
					fragmentTransaction.show(mFragments[1]).commit();
					menu.showContent();
					break;

				case R.id.rbThree:
					fragmentTransaction.show(mFragments[2]).commit();
					menu.showContent();
					break;
				case R.id.rbFour:
					fragmentTransaction.show(mFragments[3]).commit();
					menu.showContent();
					break;
				case R.id.rbFive:
					fragmentTransaction.show(mFragments[4]).commit();
					menu.showContent();
					break;
				default:
					break;
				}
			}
		});
	}

	private boolean umlogin() {
		mController.login(this, SHARE_MEDIA.SINA,
				new SocializeClientListener() {

					@Override
					public void onStart() {
						// TODO Auto-generated method stub
					}

					@Override
					public void onComplete(int arg0, SocializeEntity arg1) {
						// TODO Auto-generated method stub
						if (arg0 == 200) {
							Toast.makeText(MainActivity.this, "登陆成功",
									Toast.LENGTH_SHORT).show();
							loginflag = true;

						} else {
							Toast.makeText(MainActivity.this, "登陆失败",
									Toast.LENGTH_SHORT).show();
							loginflag = false;
						}

					}
				});
		return loginflag;
	}

	private void umgetInfo() {
		mController.getUserInfo(this, new FetchUserListener() {

			@Override
			public void onStart() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onComplete(int arg0, SocializeUser arg1) {
				// TODO Auto-generated method stub

				String uname = arg1.mLoginAccount.getUserName();
				String iconurl = arg1.mLoginAccount.getAccountIconUrl();
				Bitmap bmBitmap = toRoundBitmap.getbit(returnBitMap(iconurl));
				rimg.setImageBitmap(bmBitmap);
				tv_nick = (TextView) findViewById(R.id.tv_nick);
				tv_nick.setText(uname);
			}
		});
	}

	private Bitmap returnBitMap(String url) {
		URL myFileUrl = null;
		Bitmap bitmap = null;
		try {
			myFileUrl = new URL(url);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		try {
			HttpURLConnection conn = (HttpURLConnection) myFileUrl
					.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}
}
